package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.aspect.BaseLog;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysDict;
import com.yunc.modules.sys.service.ISysDictService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 字典码
 *
 * @author congcong tao
 * @date 2018/11/19 17:09
 */
@RestController
@RequestMapping("sysDict")
public class SysDictController extends CommonController {

    private static final Logger logger = LogManager.getLogger(SysDictController.class);

    @Autowired
    private ISysDictService sysDictService;
    
    @GetMapping("/page")
    public R page(Page<SysDict> page, SysDict sysDict){
        sysDictService.page(page, new QueryWrapper<>(sysDict));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(SysDict sysDict){
        QueryWrapper<SysDict> ew = new QueryWrapper<>();
        List<SysDict> list = sysDictService.list(ew);
        return R.ok().put("list", list).put("sysDict", sysDict);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        SysDict dict = sysDictService.getById(id);
        return R.ok().put("sysDict", dict);
    }

    /**
     * 保存
     */
    @BaseLog("字典管理")
    @PostMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@Validated @RequestBody SysDict dict){
        dict.setCreateTime(new Date());
        boolean flag = sysDictService.save(dict);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @BaseLog("字典管理")
    @PutMapping(value = "/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDict dict){
        boolean flag = sysDictService.updateById(dict);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
